package com.wing.wechat;

import com.alibaba.fastjson.JSONObject;
import com.wing.utils.HttpsUtils;
import com.wing.wechat.util.WechatUtil;
import com.wing.wechat.vo.*;
import org.springframework.stereotype.Service;

/**
 * 企业微信成员服务实现
 */
@Service
public class WechatUserServiceImpl implements WechatUserService {

    @Override
    public ResponseToken getAccessToken(String secret) {
        String requestUrl = WechatUtil.GET_TOKEN_URL.replace("REQUEST_ID", WechatUtil.ENTERPRISE_ID).replace("REQUEST_SECRET", secret);
        String result = HttpsUtils.httpsGet(requestUrl);
        ResponseToken responseToken = JSONObject.parseObject(result, ResponseToken.class);
        return responseToken;
    }

    @Override
    public ResponseCreateUser createUser(RequestCreateUser requestCreateUser) {
        ResponseToken token = getAccessToken(WechatUtil.ADDRESS_SECRET);
        String requestUrl = WechatUtil.CREATE_USER_URL.replace("ACCESS_TOKEN", token.getAccess_token());
        String result = HttpsUtils.httpsPostByJson(requestUrl, JSONObject.toJSONString(requestCreateUser));
        ResponseCreateUser responseCreateUser = JSONObject.parseObject(result, ResponseCreateUser.class);
        return responseCreateUser;
    }

    @Override
    public ResponseUpdateUser updateUser(RequestUpdateUser requestUpdateUser) {
        ResponseToken token = getAccessToken(WechatUtil.ADDRESS_SECRET);
        String requestUrl = WechatUtil.UPDATE_USER_URL.replace("ACCESS_TOKEN", token.getAccess_token());
        String result = HttpsUtils.httpsPostByJson(requestUrl, JSONObject.toJSONString(requestUpdateUser));
        ResponseUpdateUser responseUpdateUser = JSONObject.parseObject(result, ResponseUpdateUser.class);
        return responseUpdateUser;
    }

    @Override
    public ResponseDeleteUser delUser(String userId) {
        ResponseToken token = getAccessToken(WechatUtil.ADDRESS_SECRET);
        String requestUrl = WechatUtil.DELETE_USER_URL.replace("ACCESS_TOKEN", token.getAccess_token()).replace("USERID", userId + "");
        String result = HttpsUtils.httpsGet(requestUrl);
        ResponseDeleteUser responseDeleteUser = JSONObject.parseObject(result, ResponseDeleteUser.class);
        return responseDeleteUser;
    }

    @Override
    public ResponseListIdUser UserIdList(RequestListIdUser requestListIdUser) {
        ResponseToken token = getAccessToken(WechatUtil.ENTERPRISE_SECRET);
        String requestUrl = WechatUtil.LIST_ID_USER_URL.replace("ACCESS_TOKEN", token.getAccess_token());
        String result = HttpsUtils.httpsPostByJson(requestUrl,JSONObject.toJSONString(requestListIdUser));
        ResponseListIdUser listIdUser = JSONObject.parseObject(result, ResponseListIdUser.class);
        return listIdUser;
    }

    @Override
    public ResponseUserDetail userDetail(Integer userId) {
        ResponseToken token = getAccessToken(WechatUtil.ENTERPRISE_SECRET);
        String requestUrl = WechatUtil.GET_USER_URL.replace("ACCESS_TOKEN", token.getAccess_token()).replace("USERID", userId + "");
        String result = HttpsUtils.httpsGet(requestUrl);
        ResponseUserDetail responseUserDetail = JSONObject.parseObject(result, ResponseUserDetail.class);
        return responseUserDetail;
    }

    @Override
    public ResponseDepartmentUserList departmentUserList(String token, Integer departmentId) {
        String requestUrl = WechatUtil.GET_DEPARTMENT_USER_LIST_URL.replace("ACCESS_TOKEN", token).replace("DEPARTMENT_ID", departmentId + "");
        String result = HttpsUtils.httpsGet(requestUrl);
        ResponseDepartmentUserList departmentUserList = JSONObject.parseObject(result, ResponseDepartmentUserList.class);
        return departmentUserList;
    }


}
